﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
namespace QUANLYDIEMHOCSINH
{
    public partial class XemthongtinHS : Form
    {
        LOPBUS lopbus = new LOPBUS();
        public XemthongtinHS()
        {
            InitializeComponent();
            LoadDanhSachLop();
            load_ds();
            picbanh.Image = Image.FromFile("hinh\\no_picture.PNG");
        }

        void LoadDanhSachLop()
        {
            cblop.DataSource = lopbus.layDSL();
            cblop.DisplayMember = "TENLOP";
            cblop.ValueMember = "MALOP";
            cblop.SelectedItem = 0;
        }
        private void load_ds()
        {
            HOCSINHBUS hocsinhbus = new HOCSINHBUS();
            GridTTHS.DataSource = hocsinhbus.dslop(int.Parse(cblop.SelectedValue.ToString()));
            for (int i = 0; i < GridTTHS.Rows.Count; i++)
            {
                GridTTHS.Rows[i].Cells["stt"].Value = (i + 1).ToString();
            }
            GridTTHS.Columns["MAHS"].Visible = false;
            GridTTHS.Columns[3].Visible = false;
            GridTTHS.Columns[4].Visible = false;
            GridTTHS.Columns[5].Visible = false;
            GridTTHS.Columns[6].Visible = false;
            GridTTHS.Columns[7].Visible = false;
            GridTTHS.Columns[8].Visible = false;
            GridTTHS.Refresh();
        }
        private void cblop_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ds();
        }
        public static int maxoa;
        public static int ml;
       
        private void btxoa_Click(object sender, EventArgs e)
        {
            
            if(maxoa==0)
            {
                MessageBox.Show("Chưa Chọn Học Sinh!");
                return;
            }
            DialogResult result = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa?", "Thông Báo!", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                HOCSINHBUS hs = new HOCSINHBUS();
                if (hs.xoa_hs(maxoa) == 1)
                {
                    MessageBox.Show("Xóa Thành Công!");
                    load_ds();
                    maxoa = 0;
                    lbhoten.Text = "";
                    lbgioitinh.Text = "";
                    lbngaysinh.Text = "";
                    lbdiachi.Text = "";
                    lbsdt.Text = "";
                    picbanh.Image = Image.FromFile("hinh\\no_picture.PNG");
                }
            }
        }

        private void btthem_Click(object sender, EventArgs e)
        {
            ThemHS th = new ThemHS();
            th.Show();
        }

        private void btsua_Click(object sender, EventArgs e)
        {
            if (maxoa == 0)
            {
                MessageBox.Show("Chưa Chọn Học Sinh!");
                return;
            }
            chinhsuahs cs = new chinhsuahs();
            cs.Show();
        }

        private void GridTTHS_CellClick(object sender, DataGridViewCellEventArgs e)
        {  
            if (e.RowIndex >= 0)
            {
                maxoa = int.Parse(GridTTHS.Rows[e.RowIndex].Cells["MAHS"].Value.ToString());
                lbhoten.Text = GridTTHS.Rows[e.RowIndex].Cells["TENHS"].Value.ToString();
                if (GridTTHS.Rows[e.RowIndex].Cells["GIOITINH"].Value.ToString() == "1")
                {
                    lbgioitinh.Text = "Nam";
                }
                else
                {
                    lbgioitinh.Text = "Nữ";
                }
                lbngaysinh.Text = string.Format("{0:dd/MM/yyyy}", GridTTHS.Rows[e.RowIndex].Cells["NGAYSINH"].Value); 
                lbdiachi.Text = GridTTHS.Rows[e.RowIndex].Cells["DIACHI"].Value.ToString();
                lbsdt.Text = GridTTHS.Rows[e.RowIndex].Cells["SODIENTHOAIPHUHUYNH"].Value.ToString();
                string hinh = GridTTHS.Rows[e.RowIndex].Cells["ANH"].Value.ToString();
                if (hinh != "")
                {
                    picbanh.Image = Image.FromFile(GridTTHS.Rows[e.RowIndex].Cells["ANH"].Value.ToString());
                }
                else
                {
                    picbanh.Image = Image.FromFile("hinh\\no_picture.PNG");
                }
            }
        }

        private void bttimkiem_Click(object sender, EventArgs e)
        {
            HOCSINHBUS hsbus = new HOCSINHBUS();
            GridTTHS.DataSource = hsbus.TimhocSinh(txtTimKiem.Text);
            for (int i = 0; i < GridTTHS.Rows.Count; i++)
            {
                GridTTHS.Rows[i].Cells["stt"].Value = (i + 1).ToString();
            }
            GridTTHS.Refresh();
        }
    }
}
